import express from 'express';
import { MediaService } from '../services/media';

const router = express.Router();
const mediaService = new MediaService();

// 解析媒体URL
router.post('/parse', async (req, res) => {
  try {
    const { url } = req.body;
    if (!url) {
      return res.status(400).json({ error: 'URL is required' });
    }

    const result = await mediaService.parseUrl(url);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

// 获取媒体信息
router.get('/info', async (req, res) => {
  try {
    const { url } = req.query;
    if (!url) {
      return res.status(400).json({ error: 'URL is required' });
    }

    const info = await mediaService.getMediaInfo(url as string);
    res.json(info);
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

// 获取可用清晰度
router.get('/qualities', async (req, res) => {
  try {
    const { url } = req.query;
    if (!url) {
      return res.status(400).json({ error: 'URL is required' });
    }

    const qualities = await mediaService.getAvailableQualities(url as string);
    res.json({ qualities });
  } catch (error) {
    res.status(500).json({ error: (error as Error).message });
  }
});

export { router as mediaRoutes };
